﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interfaces;
using System.Data.SqlClient;

namespace Foundation
{
    public class DbController
    {
        
        private SqlCommand myCommand = new SqlCommand();
        SqlConnection myConnection;
        private static DbController instance = null;

        private DbController()
        {
        }

        public static DbController GetInstance()
        {
            if (instance == null)
            {
                instance = new DbController();
            }
            
            return instance;
        }

       
        private void OpretForbindelse()
        {
            
            try
            {
                
                string username;
                username = Environment.UserName.ToString();

                if (username == "Jakob")
                {
                    myConnection = new SqlConnection("Data Source=Jakob-Pc\\Jakob;Initial Catalog=Scooterland;Integrated Security=SSPI;Server=JAKOB-PC;Database=Scooterland;Trusted_Connection=True");
                    myConnection.Open();
                }
                else if (username == "Ole Palm")
                {
                    myConnection = new SqlConnection(//"user id=OLEPALM-THINK;" +
                        //"password=" + pw + ";" +
                                        "server=OlePalm-THINK;" +
                        // se når sql server instansen åbnes
                                        "Trusted_Connection=yes;" +
                                        "database=Scooterland; " +
                                        "connection timeout=30");
                    myConnection.Open();
                }
                else if (username == "user")
                {
                    myConnection = new SqlConnection("Data Source=WIN-9JM31EKCQ4Q\\user;Initial Catalog=Scooterland;Integrated Security=SSPI;Server=WIN-9JM31EKCQ4Q\\DMUSQLSERVER;Database=Scooterland;Trusted_Connection=True");
                    myConnection.Open();
                }
                // else
                // {
                //     connectionstring = "FEJL!";
                // }
                
            }
            catch (Exception ex)
            {
                // indsæt event om fejl som GUI kan lytte efter
            }
        }

        private void OpretSQL(string SQLsaetning)
        {
            try
            {
                SqlDataReader myReader = null;
                myCommand.Connection = myConnection;
                myCommand.CommandText = SQLsaetning;
                myReader = myCommand.ExecuteReader();

            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
            }
            finally
            {
                myConnection.Close();
            }
        }

        public void IndsaetScootermaerke(Iscootermaerke scootermaerke)
        {
            OpretForbindelse();
            string SQL = "INSERT INTO scootermaerke VALUES ('" + scootermaerke.Maerke + "')";
            OpretSQL(SQL);
        }
    }
}
